﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using NLBS.Data;
using NLBS.Entity;
using System.Web.UI.HtmlControls;
using System.Web;
using NLBS.Resources;
using NLBS.Web;
using NLBS.Data.Business;
namespace NLBS.Control
{
    public class PanelCategory : PanelModule
    {
        [TemplateContainer(typeof(DefaultCategoryTemplate))]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public override ITemplate ItemTemplate
        {
            get
            {
                return base.ItemTemplate;
            }
            set
            {
                base.ItemTemplate = value;
            }
        }

        public override string Title
        {
            get
            {
                return lang["categories"];
            }
            set
            {
                base.Title = value;
            }
        }

        protected override void CreateChildControls()
        {
            if (ItemTemplate == null)
                ItemTemplate = new DefaultCategoryTemplate();
            base.CreateChildControls();
        }
    }

    public class DefaultCategoryTemplate : ITemplate
    {
        public void InstantiateIn(System.Web.UI.Control container)
        {
            container.Controls.Add(new LiteralControl("<ul>"));
            container.Controls.Add(new LiteralControl(string.Format("<li><a href=\"{1}\" onclick=\"{2}\"><b>{0}</b></a></li>",
                Language.Instance().GetResource("index"), ReWriteUrls.Instance().Index(),
                UrlFormatsProvider.Instance().Index())));
            IList<Category> catList = CategoryData.GetCategory();
            if (catList != null)
            {
                foreach (Category cat in catList)
                {
                    container.Controls.Add(new LiteralControl("<li>"));
                    string txt = string.Format("<a href=\"{0}\" onclick=\"{4}\">{1}[{2}] {3}</a>", 
                        ReWriteUrls.Instance().ArticleCategoryUrl(cat.CategoryID), 
                        cat.Name, cat.ArticleCount.ToString(), 
                        getRssLink(cat.CategoryID),
                        UrlFormatsProvider.Instance().ArticleCategoryUrl(cat.CategoryID));
                    container.Controls.Add(new LiteralControl(txt));
                    if (cat.Locked)
                    {
                      
                        container.Controls.Add(new LiteralControl(string.Format("<img src=\"/styles/default/images/icon_lock.gif\" alt=\"{0}\" />",Language.Instance().getLanguage()["selected"])));
                    }
                    container.Controls.Add(new LiteralControl("</li>"));
                } 
            }
            container.Controls.Add(new LiteralControl("</ul>"));
        }

        public string getRssLink(int categoryID)
        {
            StringBuilder txt=new StringBuilder();
            txt.Append(string.Format("<a href=\"feed.aspx?cat={0}\" title=\"Words++ RSS Feed\" target=\"_blank\">",categoryID));
            txt.Append("<img src=\"/styles/default/images/rss.png\" class=\"meta-button\" alt=\"Words++ RSS Feed\" />");
            txt.Append("</a>");
            
            return txt.ToString();
        }
    }

}
